package com.project.base.baseproject.AppCustoms.SelectImageActivity;

import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

import com.jaeger.library.StatusBarUtil;
import com.project.base.baseproject.Activity.BaseActivity.BaseActivity;
import com.project.base.baseproject.AppCustoms.MyApplication.MyApplication;
import com.project.base.baseproject.AppCustoms.Tool.ToastUtil;
import com.project.base.baseproject.R;
import com.project.base.baseproject.Utikles.ThirdParty.RunInMainThread.UIKit;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

//多选图片 选择相册
public class MuliteSelectBucketActivity extends BaseActivity implements MuliteSelectBucketAdapter.OnRecyclerViewItemClickListener {
    //数据源
    MuliteSelectBucketAdapter muliteSelectBucketAdapter;
    //所有相册对象
    Map<String, BangBnagBucket> bangBnagBucketMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mulite_select_bucket_layout);
        StatusBarUtil.setColor(this, getResources().getColor(R.color.white),0);
        //init
        muliteSelectBucketAdapter = new MuliteSelectBucketAdapter(new ArrayList<BangBnagBucket>());
        bangBnagBucketMap = new HashMap<>();
        //配置表格视图
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.common_listview);
        RecyclerView.LayoutManager mLayoutManager2 = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        // 设置布局管理器
        recyclerView.setLayoutManager(mLayoutManager2);
        recyclerView.setAdapter(muliteSelectBucketAdapter);
        muliteSelectBucketAdapter.setOnItemClickListener(this);
        findViewById(R.id.moreselect).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        bangBnagBucketMap = getBucketList();
        ArrayList<BangBnagBucket> bangBnagBuckets = new ArrayList<>();
        for (Map.Entry<String ,BangBnagBucket> entry : bangBnagBucketMap.entrySet()) {
            bangBnagBuckets.add(entry.getValue());
        }
        muliteSelectBucketAdapter.updateData(bangBnagBuckets);
    }
    public static Map<String, BangBnagBucket> getBucketList() {
        Map<String, BangBnagBucket> bucketMap = new HashMap<>();
        ContentResolver contentResolver = MyApplication.getInstance().getContentResolver();
        try {
            String columns[] = new String[]{
                    MediaStore.Images.Media._ID,
                    MediaStore.Images.Media.BUCKET_ID,
                    MediaStore.Images.Media.DATA,
                    MediaStore.Images.Media.BUCKET_DISPLAY_NAME};
            Cursor cursor = contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, null);
            if (null == cursor) {
                return bucketMap;
            }
            if (cursor.moveToFirst()) {
                int photoPathIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                int bucketNameIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
                int bucketIdIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_ID);
                do {
                    String photoPath = cursor.getString(photoPathIndex);
                    String bucketName = cursor.getString(bucketNameIndex);
                    String bucketId = cursor.getString(bucketIdIndex);

                    BangBnagBucket bucketBean;
                    if (bucketMap.containsKey(bucketId)) {
                        bucketBean = bucketMap.get(bucketId);
                        bucketBean.setPhotoCount(bucketBean.getPhotoCount() + 1);
                    } else {
                        bucketBean = new BangBnagBucket();
                        bucketBean.setBucketId(bucketId);
                        bucketBean.setBucketName(bucketName);
                        bucketBean.setThumbPath(photoPath);
                        bucketBean.setPhotoCount(1);
                        bucketMap.put(bucketId, bucketBean);
                    }
                } while (cursor.moveToNext());
            }
        } catch (Exception e) {
            UIKit.runOnMainThreadAsync(new Runnable() {
                @Override
                public void run() {
                    ToastUtil.show(MyApplication.getInstance(),"相册获取失败");
                }
            });
        }
        return bucketMap;
    }
    @Override
    public void bucketClicked(View view, BangBnagBucket bangBnagBucket) {
        Intent intent = new Intent();
        intent.putExtra("bucketId",bangBnagBucket.getBucketId());
        setResult(RESULT_OK,intent);
        finish();
    }
}
